package geom;

public class VectorSpace {
	Vector x;
	Vector y; 
	Vector z;
	Vector offset;
	
	private VectorSpace() {
		Vector x = new Vector(1, 0, 0);
		Vector y = new Vector(0, 1, 0);
		Vector z = new Vector(0, 0, 1);
		offset = new Vector();
	}
	
	public static VectorSpace newVectorSpaceFromCurveEnd(Vector v0, Vector v1, Vector v2){
		VectorSpace space = new VectorSpace();
		if(!v0.equalTo(v1) && !v2.equalTo(v1)){
			space.x = v2.sub(v1).normalize();
			space.y = v1.sub(v0).xProdut(v2.sub(v1)).normalize();
			space.z = space.x.xProdut(space.y).normalize();
			space.offset = v2.getCopy();
			return space;
		}
		
		return null;
	}
	
	public static VectorSpace newVectorSpaceFromCurveStart(Vector v2, Vector v3, Vector v4){
		VectorSpace space = new VectorSpace();
		if(!v2.equalTo(v3) && !v3.equalTo(v4)){
			space.x = v3.sub(v2).normalize();
			space.y = v4.sub(v3).xProdut(v3.sub(v2)).normalize();
			space.z = space.x.xProdut(space.y).normalize();
			space.offset = v2.getCopy();
			return space;
		}
		
		return null;
	}
	
	public static VectorSpace newVectorSpaceFromCurve(Vector v1, Vector v2, Vector v3){
		VectorSpace space = new VectorSpace();
		if(!v1.equalTo(v2) && !v2.equalTo(v3)){
			space.x = v3.sub(v1).normalize();
			space.y = v2.sub(v1).xProdut(v3.sub(v2)).normalize();
			space.z = space.x.xProdut(space.y).normalize();
			space.offset = v2.getCopy();
			return space;
		}
		else return null;
	}
	
	public static VectorSpace newVectorSpaceFromCurveAndCenter(Vector v1, Vector v2, Vector v3, Vector center){
		VectorSpace space = new VectorSpace();
		//if(v1.equalTo(center)){
		//	return new VectorSpace();
		//}
		space.x = v3.sub(v1).normalize();
		space.y = space.x.xProdut(v2).normalize();
		space.z = space.x.xProdut(space.y).normalize();
		space.offset = v2.getCopy();
		return space;

	}

	public Vector getOrthoCoord(Vector v){
		
		Vector o = x.mult(v.x);
		o.addMe(y.mult(v.y));
		o.addMe(z.mult(v.z));
		o.addMe(offset);
		return o;
	}

	public Vector getOffset() {
		return offset;
	}

	public Vector getX() {
		return x;
	}

	public Vector getY() {
		return y;
	}

	public Vector getZ() {
		return z;
	}

	public void setOffset(Vector offset) {
		this.offset = offset;
	}

	public void setX(Vector x) {
		this.x = x;
	}

	public void setY(Vector y) {
		this.y = y;
	}

	public void setZ(Vector z) {
		this.z = z;
	}
	
	
	

}
